
Vue.prototype.$http = axios
axios.defaults.baseURL='http://127.0.0.1:30010/services'

//解析url参数函数 http://localhost/infor.html?id=423&name=xxx
// {id:426,name:"xxx"}
function parseParamObj(url) {
    let paramsStr = url.substring(url.indexOf("?")+1); //typeId=27
    let paramsArray = paramsStr.split("&"); //typeId=27 xxx=xxx
    let paramObj = {};
    $.each(paramsArray,function(index,param){
        let parmName = param.split("=")[0];
        let parmValue = param.split("=")[1];
        paramObj[parmName] = parmValue;
    })
    return paramObj;
}

function generateUUID() {
    var d = new Date().getTime(); //Timestamp
    var d2 = (performance && performance.now && (performance.now()*1000)) || 0; //Time in microseconds since page-load or 0 if unsupported
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16; //random number between 0 and 16
        if(d > 0) { //Use timestamp until depleted
            r = (d + r)%16 | 0;
            d = Math.floor(d/16);
        } else { //Use microseconds since page-load if supported
            r = (d2 + r)%16 | 0;
            d2 = Math.floor(d2/16);
        }
        return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
}


// axios请求拦截器:添加请求拦截器，在请求头中加token
axios.interceptors.request.use(
    config => {
        // 从localStorage中获取token
        let token = localStorage.getItem("token");

        // 如果token有值,我们就放到请求头里面
        if (token) {
            config.headers.token = token;
        }
        return config
    },
    error => {
        return Promise.reject(error)
    })

// 响应拦截器
axios.interceptors.response.use(function (response) {
    //对返回的数据进行操作
    let data = response.data
    let result = data;  // response.data 就是后端返给我们的数据
    if (!result.success && result.msg == "login failed") { // 说明未登录,被拦截了,那么就要跳回到登陆页面
        location.href="index.html";  // 跳转回登陆页面,让用户登陆
    }
    if (!result.success && result.msg == "noPermission") { // 说明未登录,被拦截了,那么就要跳回到登陆页面
        location.href="index.html";  // 跳转回登陆页面,让用户登陆
    }
    return response
}, function (err) {
    return Promise.reject(err)
})

